//
//  VipTimeView.m
//  intelligent
//
//  Created by 甘世清 on 2024/5/22.
//

#import "VipTimeView.h"
#import "MeVipViewController.h"
@implementation VipTimeView
- (void)dealloc{
    [LSTTimer removeTimerForIdentifier:@"VIPTimeView"];
}
- (void)SQ_UIViewForSelfView{
    UILabel * toplabel = [[UILabel alloc]init];
    toplabel.text = SQNSLocalString(@"你选择的套餐正在超级折扣");
    toplabel.font = FONT_MEDIUM(13);
    toplabel.textAlignment = NSTextAlignmentCenter;
    toplabel.textColor = SQColor(@"#80A9FF");
    [self addSubview:toplabel];
    [toplabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(4);
    }];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_bottom).offset(-4);
    }];
   
    [self SQ_time];
}
-(void)SQ_time{
    UIViewController * vc = [Manager SQ_getCurrentViewController];
    if (![vc isKindOfClass:[MeVipViewController class]]) {
        [LSTTimer removeTimerForIdentifier:@"VIPTimeView"];
        return;
    }
    NSDate * endTime = nil;
    NSDate * newTime = [NSDate br_dateFromString:[NSString getTheCurrentYMD] dateFormat:@"yyyy-MM-dd"];
    endTime = [newTime br_getNewDateToDays:1];
    if (endTime != nil && newTime != nil) {
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        NSTimeInterval endtime = [endTime timeIntervalSince1970];
        NSTimeInterval  shengyu = endtime - time;
        WS(weakSelf);
        [LSTTimer addTimerForTime:shengyu identifier:@"VIPTimeView" handle:^(NSString * _Nonnull day, NSString * _Nonnull hour, NSString * _Nonnull minute, NSString * _Nonnull second, NSString * _Nonnull ms) {
            weakSelf.timeLabel.text =[NSString stringWithFormat:@"%@%@小时%@分钟%@秒",SQNSLocalString(@"优惠倒计时："),hour,minute,second];
            weakSelf.timeLabel.keywords = SQNSLocalString(@"优惠倒计时：");
            weakSelf.timeLabel.keywordsColor = SQColor333333;
            weakSelf.timeLabel.keywordsFont = FONT_MEDIUM(12);
            [weakSelf.timeLabel getLableSizeWithMaxWidth:200];
        } finish:^(NSString * _Nonnull identifier) {
            [LSTTimer removeTimerForIdentifier:@"VIPTimeView"];
            [weakSelf SQ_time];
        } pause:^(NSString * _Nonnull identifier) {
            
        }];
    }
    
}

-(void)SQ_showOrHid:(BOOL)isShow{
    if (isShow) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(50);
        }];
    } else {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(10);
        }];
    }
    [self layoutIfNeeded];
    self.hidden = !isShow;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = FONT_MEDIUM(15);
        _timeLabel.textColor = SQColor(@"#FFAB49");
        _timeLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _timeLabel;
}
@end
